package com.qust.rest.configs;

import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @author Frank KONG
 * @version 1.0
 * @date 2021/11/22 20:34
 */
@Configuration
@EnableSwagger2
@ConditionalOnExpression("${swagger2.enable}")
public class SwaggerConfig {

    /**
     * 配置文件读取是否开启swagger
     * 生产环境关闭swagger
     */
    @Value("${swagger2.enable}")
    private boolean enable;

    @Bean
    public Docket createDocket(){

        //单点登录
        List<Parameter> parameters = Lists.newArrayList(new ParameterBuilder()
                .name("sso-token")
                .description("用户登录认证token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(true)
                .build());

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(parameters)
                //进入swagger-ui的信息
                .apiInfo(apiInfo())
                .select()
                //暴露所有controller类的所在的包路径
                .apis(RequestHandlerSelectors.basePackage("com.qust.rest.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    //进入swagger-ui的信息
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                //该项目的名字
                .title("Swagger for AI System")
                //该项目的描述
                .description("前后端分离项目")
                .version("1.0.0")
                .build();
    }

}
